display.setStatusBar( displayHiddenStatusBar ) -- sumindo com a barra de cima do SO
-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------
local storyboard = require ( "storyboard" )


--###############DEFINIÇÕES GERAIS########################################
_W = display.viewableContentWidth
_H = display.viewableContentHeight
local posicao_attrX = 289 --posição x do primeiro atributo
local posicao_attrY = 35  --posicao y do primeiro atributo
local center_x = display.contentCenterX --variaveis que definem o centro da tela no eixo X
local center_y = display.contentCenterY

-- ############# BACKGROUND ##################################################
local content_height	= display.contentHeight
local background		= display.newImage("images/fundo2.jpg")

-- centralizando fundo dando mais espaço embaixo
background.y			= display.contentWidth / 1.5

--############## CARREGAMENTO DE DADOS JSON ##################################
local json = require("json")
function load_file( filename )
    -- set default base dir if none specified
    local base = system.ResourceDirectory
 
    -- create a file path for corona i/o
    local path = system.pathForFile( filename, base )
 
    -- will hold contents of file
    local contents
 
    -- io.open opens a file at path. returns nil if no file found
    local file = io.open( path, "r" )
    if file then
        -- read all contents of file into a string
        contents = file:read( "*a" )
        io.close( file )    -- close the file after using it
        --return decoded json string
        return json.decode( contents )
    else
        --or return nil if file didn't ex
        return nil
    end
end

function save( filename, dataTable )
    --encode table into json string
    local jsonString = json.encode( dataTable )
    -- create a file path for corona i/o
    local path = system.pathForFile( filename, system.ResourceDirectory )
    -- io.open opens a file at path. Creates one if doesn't exist
    local file = io.open( path, "w" )
    if file then
        --write json string into file
       file:write( jsonString )
       -- close the file after using it
       io.close( file )
    end
end

local player = load_file("dados_player.json")
local estoria = load_file("historia_exemplo.json")


--###################BOTÕES DE ESCOLHA DE LINK ###################################################
local widget = require("widget")
local font = "HelveticaNeue" or native.systemFont;

local function btnOnEventHandler(e)
	

end

local btn1 = widget.newButton({
	id = "botao 1",
	left = 0,
	top = 0,
	label = estoria.capitulo.links.link1.texto,
	width = 256,
	height = 36,
	cornerRadius = 8,
	font = font,
	fontSize = 14,
	onEvent = btnOnEventHandler
});

btn1.x = _W * 0.5;
btn1.y = _H - 190;

local btn2 = widget.newButton({
	id = "botao 2",
	left = 0,
	top = 0,
	label = estoria.capitulo.links.link1.texto,
	width = 256,
	height = 36,
	cornerRadius = 8,
	font = font,
	fontSize = 14,
	onEvent = btnOnEventHandler
});

btn2.x = _W * 0.5;
btn2.y = _H - 140;

local btn3 = widget.newButton({
	id = "botao 3",
	left = 0,
	top = 0,
	label = estoria.capitulo.links.link1.texto,
	width = 256,
	height = 36,
	cornerRadius = 8,
	font = font,
	fontSize = 14,
	onEvent = btnOnEventHandler
});

btn3.x = _W * 0.5;
btn3.y = _H - 90;




--################MENUBAR INFERIOR - FICHA DO PERSONAGEM, ATRIBUTOS E ITENS########################################
local group_itens		= display.newGroup()
local group_bar			= display.newGroup()

local imageItens		= display.newImage( "images/base002.png" )
local imageMenu			= display.newImage( "images/base001.png" )
local imageMonitor		= display.newImage( "images/monitor.png" )
local personagem		= display.newImage( "images/personagem.png" )
local moedas			= display.newImage( "images/moedas2.png" )
local pos_inicial		= content_height - 110
local pos_inicial_itens	= pos_inicial - 20

imageMonitor.y	= 68
imageMonitor.x	= 135

personagem.y	= 75
personagem.x	= 60

moedas.y		= 115
moedas.x		= 160

group_bar[ "minimizado" ]	= false
group_itens[ "minimizado" ]	= true

group_itens:insert( imageItens )
group_itens:insert( imageMonitor )
group_itens.y = pos_inicial_itens
group_itens.x = 27;

group_bar:insert( imageMenu )
group_bar:insert( personagem )
group_bar:insert( moedas )
group_bar.y = pos_inicial

habilidade_count = display.newText{
	parent = group_bar,
	x = posicao_attrX,
	y = posicao_attrY,
	text = player.atributos.habilidade,
	fontSize = 20
}

ataque_count = display.newText{
	parent = group_bar,
	x = posicao_attrX,
	y = posicao_attrY + 25,
	text = player.atributos.ataque,
	fontSize = 20
}

energia_count = display.newText{
	parent = group_bar,
	x = posicao_attrX,
	y = posicao_attrY + 50,
	text = player.atributos.energia,
	fontSize = 20
}

sorte_count = display.newText{
	parent = group_bar,
	x = posicao_attrX,
	y = posicao_attrY + 75,
	text = player.atributos.sorte,
	fontSize = 20
}

moedas_count = display.newText{
	parent = group_bar,
	x = 160,
	y = 115,
	text = "$$ "..player.moedas,
	fontSize = 12
}

moedas_count:setTextColor( 110, 203, 20 )

function gera_item(nome, qtd ,pos)
	item = display.newText{
		parent = group_itens,
		x = 135,
		y = 45 + ( pos * 10 ),
		text = nome.."("..qtd..")",
		fontSize = 10
	}
	item:setTextColor( 110, 203, 20 )
	return item
end

local count = 1

for key, value in pairs( player.itens ) do
	gera_item( key, value, count )
	count = count + 1
end


--################ AUDIO ######################################	
audio.setVolume(0.5)
deslize_som = audio.loadSound("sounds/deslize.mp3")
bgSound = audio.loadStream("sounds/back.mp3") --adicionando o som a variavel
audio.play(bgSound, {loops = -1}) --enquanto for -1 o loop fica infinito

local function colapsaMenu( event )
	if( event.phase == "began" ) then
		if not group_bar.minimizado then
			transition.to( group_bar, { time=200, delay=100, y = content_height, onStart = audio.play(deslize_som)})
			transition.to( group_itens, { time=200, delay=100, y = ( content_height - 20 )})
			group_bar.minimizado = true 
		else
			transition.to( group_bar, { time=200, delay=100, y = pos_inicial, onStart = audio.play(deslize_som)})
			transition.to( group_itens, { time=200, delay=100, y = pos_inicial - 20})
			group_bar.minimizado = false
		end
	end
	return true
end

imageMenu:addEventListener( "touch", colapsaMenu )

local function colapsaMenuItens( event )
	if( event.phase == "began" ) then
		if not group_bar.minimizado then
			if not group_itens.minimizado then
				transition.to( group_itens, { time=200, delay=100, y = pos_inicial_itens})
				group_itens.minimizado = true 
			else
				transition.to( group_itens, { time=200, delay=100, y = pos_inicial_itens - 105})
				group_itens.minimizado = false
			end
		end
	end
	return true
end

imageItens:addEventListener( "touch", colapsaMenuItens )

local textObj = display.newText("A short string", 0, 20, display.contentWidth - 40, display.contentHeight * 0.5, nil, 16);
textObj:setReferencePoint(display.CenterReferencePoint);
textObj.x = display.contentCenterX;
textObj:setTextColor( 49, 49, 49 )
textObj.text = estoria.capitulo.texto
textObj.x = display.contentCenterX;